package com.example.jdimust.upload;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.jdimust.util.ACache;
import com.example.jdimust.register.NetworkRequest;
import com.example.jdimust.R;
import com.example.jdimust.value.ValueActivity;
import com.example.jdimust.login.LoginActivity;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.MediaType;

/**
 * Author: MaZongyang
 * Date: 2021/5/21/0021 9:16
 * Description:更新数据
 */
public class UploadUpdateActivity extends AppCompatActivity {
    private TextView upledname, upaccname, uptype, upkeyvalue, upnowdata;
    private EditText up_data;
    private Button bt_updata;
    private ACache acache;//缓存框架

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectCustomSlowCalls() //API等级11，使用StrictMode.noteSlowCode 自定义的耗时调用
                .detectDiskReads() //磁盘读取操作
                .detectDiskWrites() //磁盘写入操作
                .detectNetwork()  //网络操作
                //  .penaltyDialog() //弹出违规提示对话框
                .detectAll()
                .penaltyLog() //在Logcat 中打印违规异常信息
                .build());

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll()
                .detectActivityLeaks()  //Activity泄露
                .detectLeakedSqlLiteObjects()  //泄露的Sqlite对象
                .setClassInstanceLimit(this.getClass(), 1) //检测实例数量
                .penaltyLog()
                .build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata);

        Intent intent = getIntent();
//        String valuekey = intent.getStringExtra("valuekey");
//        String todata = intent.getStringExtra("todata");
//        String totype = intent.getStringExtra("totype");
//        String oldvalue = intent.getStringExtra("value");
        acache = ACache.get(this);//创建ACache组件
//        acache.getAsString("ledgerHash")
        upledname = (TextView) findViewById(R.id.updata_ledhash);
        upaccname = (TextView) findViewById(R.id.updata_accname);
        upnowdata = (TextView) findViewById(R.id.updata_value);
        uptype = (TextView) findViewById(R.id.updata_type);
        upkeyvalue = (TextView) findViewById(R.id.updata_keyvalue);
        up_data = (EditText) findViewById(R.id.updata_data);
        bt_updata = (Button) findViewById(R.id.bt_updata);
        upledname.setText(acache.getAsString("acaledgerName"));
        upaccname.setText(acache.getAsString("acaaccountName"));
        System.out.println(acache.getAsString("acaledgerName"));
        uptype.setText(acache.getAsString("acatotype"));
//        uptype.setText(totype);
        upkeyvalue.setText(acache.getAsString("acavaluekey"));
//        upkeyvalue.setText(valuekey);
        upnowdata.setText(acache.getAsString("acavalue"));
//        upnowdata.setText(oldvalue);

        bt_updata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String updata_data1 = up_data.getText().toString();
                if (updata_data1.length() == 0) {
                    updata_data1 = "";
                }
                String updatajson = "{\n" +
                        " \"ledgerHash\": \"" + acache.getAsString("acaledgerHash") + "\",\n" +
                        " \"updateValue\": [\n" +
                        " {\n" +
                        " \"accountHash\": \"" + acache.getAsString("accounthash") + "\",\n" +
                        " \"type\": \"" + acache.getAsString("acatotype") + "\",\n" +
                        " \"key\": \"" + acache.getAsString("acavaluekey") + "\",\n" +
                        " \"values\": " + updata_data1 + "\n" +
                        " }\n" +
                        " ]" +
                        " }";
                System.out.println("updatajson---------------------------\n" + updatajson + "\n---------------------------");
                MediaType header = MediaType.parse("application/json;charset=utf-8");
                String rejson = null;
                try {
                    rejson = NetworkRequest.postaccount("/Upload", updatajson, header);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("rejson: " + rejson);
                Gson gson1 = new Gson();
                UploadBean reupdatabean = gson1.fromJson(rejson, UploadBean.class);
                String updatamed = reupdatabean.getMessage();
                AlertDialog alert = new AlertDialog.Builder(UploadUpdateActivity.this).create();
//               alert.setIcon(R.drawable.stop);
                alert.setTitle("提示");
                alert.setMessage(updatamed);
                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                  Intent intent = new Intent(UpdateActivity.this, DashboardFragment.class);
//                      startActivity(intent);
//                        Intent intent = new Intent(UploadUpdateActivity.this, ValueActivity.class);//从SpendingActivity页面跳转至ExpenseProcesActivity页面
                        startActivity(new Intent(UploadUpdateActivity.this, ValueActivity.class));
                        finish();
                    }
                });
                alert.show();
//                showPopup("提示", updatamed, "返回");
                Toast.makeText(UploadUpdateActivity.this, "已点击按钮", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void showPopup(String show_title, String show_message, String show_button) {
        new AlertDialog.Builder(this)
                .setTitle(show_title)
                .setMessage(show_message)
                .setPositiveButton(show_button, null)
                .show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    /**
     * 菜单的点击事件
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.chageuser:
                Toast.makeText(this, "正在切换用户！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(UploadUpdateActivity.this, LoginActivity.class);//从SpendingActivity页面跳转至ExpenseProcesActivity页面
                startActivity(intent);
                break;
            default:
                break;
        }

        return true;
    }
}
